package com.example.listener;

import com.example.settings.domain.DicValue;
import com.example.settings.service.DicService;
import com.example.settings.service.impl.DicServiceImpl;
import com.example.utils.ServiceFactory;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.*;

public class SysInitListener implements ServletContextListener {
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("服务器开启，监听上下文作用域");
        System.out.println("开始从后台获取数据字典");
        ServletContext application=sce.getServletContext();
        DicService dicService = (DicService) ServiceFactory.getService(new DicServiceImpl());
        Map<String, List<DicValue>> map =dicService.getDic();
        Set<String> set = map.keySet();
        //System.out.println(set);
        for (String key:set) {
            //System.out.println("存入的key是----------------------"+key);
            application.setAttribute(key,map.get(key));
        }
        System.out.println("数据字典成功存入缓存");


        Map<String,String> pmap = new HashMap<String, String>();
        ResourceBundle bundle = ResourceBundle.getBundle("Stage2Possibility");
        Enumeration<String> e = bundle.getKeys();
        while (e.hasMoreElements()){

            //获得阶段
            String key = e.nextElement();
            //可能性
            String value = bundle.getString(key);
            pmap.put(key,value);
        }

        application.setAttribute("pMap",pmap);
    }
}
